from flask import Flask, render_template, redirect, request, session, url_for
import config
from exts import db
from models import User, WeekReport, ProNum, Staff, CustUnit, Depart, Duty
from decorators import login_required


app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/login/', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    else:
        email = request.form.get('email')
        password = request.form.get('password')
        user = User.query.filter(User.email == email, User.password == password).first()
        if user:
            session['user_id'] = user.id
            session.permanent = True
            return redirect(url_for('index'))
        else:
            return '邮箱或密码错误，请确认后登录！'


@app.route('/regist/', methods=['GET', 'POST'])
def regist():
    if request.method == 'GET':
        return render_template('regist.html')
    else:
        username = request.form.get('username')
        email = request.form.get('email')
        password1 = request.form.get('password1')
        password2 = request.form.get('password2')

        user = User.query.filter(User.email == email).first()
        if user:
            return '该邮箱已经被注册，请更换邮箱'
        else:
            # 验证两次输入密码是否一致
            if password1 != password2:
                return '两次输入密码不一致'
            else:
                user = User(username=username, email=email, password=password1)

                db.session.add(user)
                db.session.commit()

                # 如果注册成功，跳转到登录界面
                return redirect(url_for('login'))


@app.route('/logout/')
def logout():
    session.pop('user_id')
    return redirect(url_for('login'))


@app.route('/zblist/', methods=['GET', 'POST'])
@login_required
def zblist():
    if request.method == 'GET':
        return render_template('zblist.html')
    else:
        pass


@app.route('/custlist/', methods=['GET', 'POST'])
@login_required
def custlist():
    context = {
        'custs': CustUnit.query.all()
    }
    return render_template('custlist.html', **context)


@app.route('/newcust/', methods=['GET', 'POST'])
@login_required
def newcust():
    if request.method == 'GET':
        return render_template('newcust.html')
    else:
        custunit_name = request.form.get('custunit_name')
        cust = CustUnit(custunit_name=custunit_name)
        db.session.add(cust)
        db.session.commit()
        return redirect(url_for('custlist'))


@app.route('/newnum/', methods=['GET', 'POST'])
@login_required
def newnum():
    if request.method == 'GET':
        staff_selects = Staff.query.all()
        cust_selects = CustUnit.query.all()
        return render_template('newnum.html', staff_selects=staff_selects, cust_selects=cust_selects)
    else:
        project_num = request.form.get('project_num')
        pro_name = request.form.get('pro_name')
        custunit = request.values.get('cust_select')
        saleshead = request.values.get('sale_select')
        techhead = request.values.get('tech_select')
        develophead = request.values.get('develop_select')
        beizhu = request.form.get('beizhu')
        pronum = ProNum(project_num=project_num, pro_name=pro_name, custunit=custunit, saleshead=saleshead, techhead=techhead, develophead=develophead, beizhu=beizhu)
        db.session.add(pronum)
        db.session.commit()
        return redirect(url_for('numlist'))



@app.route('/numlist/', methods=['GET', 'POST'])
@login_required
def numlist():
    pronums = ProNum.query.all()
    for pronum in pronums:
        staff_sale= Staff.query.filter(Staff.id == pronum.saleshead).first()
        staff_tech= Staff.query.filter(Staff.id == pronum.techhead).first()
        staff_develop= Staff.query.filter(Staff.id == pronum.develophead).first()
        cust_display = CustUnit.query.filter(CustUnit.id == pronum.custunit).first()

        if cust_display:
            pronum.custunit = cust_display.custunit_name
        else:
            pronum.custunit = '暂无'

        if staff_sale:
            pronum.saleshead = staff_sale.staff_name
        else:
            pronum.saleshead = '王玮'

        if staff_tech:
            pronum.techhead = staff_tech.staff_name
        else:
            pronum.techhead = '王志刚'

        if staff_develop:
            pronum.develophead = staff_develop.staff_name
        else:
            pronum.develophead = '史晓刚'
    # context = {
    #     # 'pronums': ProNum.query.all()
    #     'pronums'
    # }
    return render_template('numlist.html', pronums=pronums)


@app.route('/stafflist/', methods=['GET', 'POST'])
@login_required
def stafflist():
    staffs = Staff.query.all()
    for staff in staffs:
        depart = Depart.query.filter(Depart.id ==staff.depart).first()
        if depart:
            staff.depart = depart.depart_name
        else:
            staff.depart = '暂无'

        duty = Duty.query.filter(Duty.id==staff.staff_duty).first()
        if duty:
            staff.staff_duty = duty.duty_name
        else:
            staff.staff_duty = '暂无'
    return render_template('stafflist.html', staffs=staffs)


@app.route('/addstaff/', methods=['GET', 'POST'])
@login_required
def addstaff():
    if request.method == 'GET':
        context = {
            'departs':Depart.query.all(),
            'dutys':Duty.query.all(),
        }
        return render_template('addstaff.html', **context)
    else:
        staff_id = request.form.get('staff_id')
        staff_name = request.form.get('staff_name')
        staff_duty = request.values.get('duty_select')
        depart = request.values.get('depart_select')
        staff_content = Staff(staff_num=staff_id, staff_name=staff_name, staff_duty=staff_duty, depart=depart)
        db.session.add(staff_content)
        db.session.commit()
        return redirect(url_for('stafflist'))


@app.route('/departlist/', methods=['GET', 'POST'])
@login_required
def departlist():
    departs = Depart.query.all()
    for depart in departs:
        staff_header = Staff.query.filter(Staff.id == depart.depart_header).first()
        if staff_header:
            depart.depart_header = staff_header.staff_name
        else:
            depart.depart_header = '暂无'

    return render_template('departlist.html', departs=departs)


@app.route('/newdepart/', methods=['GET', 'POST'])
@login_required
def newdepart():
    if request.method == 'GET':
        staff_selects = Staff.query.all()
        return render_template('newdepart.html', staff_selects=staff_selects)
    else:
        depart_name = request.form.get('depart_name')
        depart_header = request.values.get('staff_select')
        depart_content = Depart(depart_name=depart_name, depart_header=depart_header)
        db.session.add(depart_content)
        db.session.commit()
        return redirect(url_for('departlist'))


@app.route('/dutylist/', methods=['GET', 'POST'])
@login_required
def dutylist():
    context = {
        'dutys': Duty.query.all()
    }
    return render_template('dutylist.html', **context)


@app.route('/newduty/', methods=['GET', 'POST'])
@login_required
def newduty():
    if request.method == 'GET':
        return render_template('newduty.html')
    else:
        duty_name = request.form.get('duty_name')
        duty_desc = request.form.get('duty_desc')
        duty_content = Duty(duty_name=duty_name, duty_desc=duty_desc)
        db.session.add(duty_content)
        db.session.commit()
        return redirect(url_for('dutylist'))


@app.context_processor
def my_context_processor():
    user_id = session.get('user_id')
    if user_id:
        user = User.query.filter(User.id == user_id).first()
        if user:
            return {'user': user}
    return{}


if __name__ == '__main__':
    app.run()
